package com.zhengx.fragmentutildemo;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.zhengx.fragmentutil.BaseQuickFragment;
import com.zhengx.fragmentutildemo.pager.FragmentPagerOne;
import com.zhengx.fragmentutildemo.pager.FragmentPagerTwo;

/**
 * name：com.zhengx.fragmentutildemo
 * class: describe
 * author: zhengx
 * create_time: 2019/5/7
 */
public class FragmentTwo extends BaseQuickFragment {

    private ViewPager viewPager;

    private Fragment[] fragments;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fragments = new Fragment[]{new FragmentPagerOne(), new FragmentPagerTwo()};
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_fragment_two, container, false);
        viewPager = view.findViewById(R.id.view_pager);
        viewPager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        }

        @Override
        public Fragment getItem(int i) {
            return fragments[i];
        }

        @Override
        public int getCount() {
            return fragments.length;
        }

    }

//    @Override
//    public boolean onInterceptBackPressed() {
//        FragmentUtil.findController(this).pop();
//        return true;
//    }
}
